﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using Cubicle.Core.Interfaces;
using Cubicle.Core.Processors;

namespace Cubicle.Excel.Processors
{
    [Export("Cubicle.Excel.Processors.MarshalArrayToObject", typeof(IProcessor<object>))]
    [ExportMetadata("Name", "MarshalArrayToObject")]
    [ExportMetadata("Description", "Convert T[] to object[]")]
    public class MarshalArrayToObject : ConditionalProcessor<object>
    {

        public override bool Condition(object request)
        {
            var type = request.GetType();
            return type.IsArray && type.GetArrayRank() == 1;
        }

        public override object ConditionalProcess(object request)
        {
            var arr = (Array)request;
            return arr.Cast<object>().ToArray();
        }

    }
}